<template>
  <div class="app-container">
    <div class="header-box">
      <el-form class="header-left" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
        label-width="100px">
        <el-form-item label="商户名称" prop="storeName">
          <el-input v-model="queryParams.storeName" placeholder="请输入商户名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker style="width: 210px;" v-model="daterangeCreateTime" value-format="yyyy-MM-dd" type="daterange"
            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="代理商名称" prop="agentName">
          <el-input v-model="queryParams.agentName" placeholder="请输入代理商名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
      </el-form>
      <div class="btn-box">
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <el-divider></el-divider>
    <div class="button-right">
      <el-button size="small" type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>
      <el-button type="primary" size="small" icon="el-icon-coin" @click="handleRecharge">充值</el-button>
    </div>
    <el-table v-loading="loading" :data="storeAddList">
      <el-table-column label="序号" align="center" type="index" width="80" />
      <el-table-column label="商家名称" align="center" prop="storeName" />
      <el-table-column label="所属代理商" align="center" prop="agentName" />
      <el-table-column label="充值金额" align="center" prop="exchangeAmount" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="操作人" align="center" prop="userName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="text" icon="el-icon-view" @click="handleUpdate(scope.row)"
            v-hasPermi="['agentAdd:agentAdd:edit']">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商户充值订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="商家名称">
          <el-input v-model="form.storeName" disabled />
        </el-form-item>
        <el-form-item label="所属代理商">
          <el-input v-model="form.agentName" disabled />
        </el-form-item>
        <el-form-item label="充值金额">
          <el-input v-model="form.exchangeAmount" disabled />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-input v-model="form.createTime" disabled />
        </el-form-item>
        <!--        <el-form-item label="交易信息，展示对应的订单编号和套餐名称、候选人数量、操作人名称">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item> -->
        <!--        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStoreAdd, getStoreAdd, delStoreAdd, addStoreAdd, updateStoreAdd } from "@/api/storeAdd/storeAdd";

export default {
  name: "StoreAdd",
  data() {
    return {
      daterangeCreateTime: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户充值订单表格数据
      storeAddList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        agentName: null,
        createTime: null,
        createBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        exchangeMark: [
          { required: true, message: "交易标识id，十位数字和字母组成不能为空", trigger: "blur" }
        ],
        storeId: [
          { required: true, message: "商户id不能为空", trigger: "blur" }
        ],
        agentName: [
          { required: true, message: "代理商名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "消费类型 0代理商充值 1商家充值 2商家发起背调扣费 3对应代理商接口调用扣费不能为空", trigger: "change" }
        ],
        exchangeAmount: [
          { required: true, message: "交易金钱数额不能为空", trigger: "blur" }
        ],
        balance: [
          { required: true, message: "交易后余额不能为空", trigger: "blur" }
        ],
        // userId: [
        //   { required: true, message: "用户id不能为空", trigger: "blur" }
        // ],
        // deptId: [
        //   { required: true, message: "部门id不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  activated(){
    this.getList();
  },
  methods: {
    /** 查询商户充值订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listStoreAdd(this.queryParams).then(response => {
        this.storeAddList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        exchangeMark: null,
        storeId: null,
        storeName: null,
        agentName: null,
        type: null,
        exchangeAmount: null,
        balance: null,
        content: null,
        dataId: null,
        imgUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.id)
    //   this.single = selection.length!==1
    //   this.multiple = !selection.length
    // },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加商户充值订单";
    // },
    /** 修改按钮操作  -----改成查看了 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStoreAdd(id).then(response => {
        this.form = response.data;
        this.open = true;
        // this.title = "修改商户充值订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.open = false;
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != null) {
      //       updateStoreAdd(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addStoreAdd(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除商户充值订单编号为"' + ids + '"的数据项？').then(function() {
    //     return delStoreAdd(ids);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {});
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
      this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      this.download('storeAdd/storeAdd/export', {
        ...this.queryParams
      }, `storeAdd_${new Date().getTime()}.xlsx`)
    },
    handleRecharge() {
      this.$router.push({ path: '/order/TopupOrder', query: { info: 'Store' } });
    }
  }
};
</script>

<style lang="scss" scoped>
.el-divider {
  margin-top: 50px;
}
</style>
